package com.itheima.controller;

import com.itheima.domain.RecordSelective;
import com.itheima.domain.User;
import com.itheima.service.RecordService;
import com.itheima.util.RequestSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * @Author: auuuu4
 * @Date: 2024/07/10/11:02
 * @Description:
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    RecordService recordService;
    @RequestMapping("/searchRecords")
    public void searchRecords(String borrower,String bookname,Integer pageNum,Integer pageSize, HttpServletRequest request, HttpServletResponse response){
        try {
            User user = RequestSessionUtil.getUserInfo(request);
            RecordSelective selective = new RecordSelective(borrower,user.getName(),bookname);
            if(user.getRole().equals("ADMIN"))
                selective.setUsername(null);

            RequestSessionUtil.setAttribute(request,"pageResult",recordService.selectRecordsSelective(pageNum,pageSize, selective));
            request.getRequestDispatcher("/admin/record.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();

        }
    }
}
